package cts;

import houseHold.Room;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.BevelBorder;

import done.TemperatureScale;

import sensor.Sensor;
import sensor.SensorType;
import simulation.WriterCollection;

public class SensorPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private static final int d_height=100;
	private static final int d_width=800;
	private Vector<Sensor> list = new Vector<Sensor>();

	/**
	 * This is the default constructor
	 */
	public SensorPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(900, 400);
		this.setLayout(new GridBagLayout());
	}

  //  @jve:decl-index=0:

	public void addSensors(Vector<Sensor> sensorList) {
		list.addAll(sensorList);
	}

	public void build() {
		components=new Vector<JScrollPane>();
		for (int i = 0; i < list.size(); i++) {
			Sensor s = list.get(i);
			switch (s.getType()) {
			case WINDOW_SENSOR:
				break;
			case GAS_SENSOR:
				break;
			case WATER_SENSOR:
				break;
			case ELECTRIC_SENSOR:
				break;
			case TEMPERATURE_SENSOR:
				buildTemp(s);
				break;
			case LIGHT_SENSOR:
				break;
			case PR_SENSOR:
				break;
			}
		}
		for(int i=0;i<components.size();i++)
		{
			this.add(components.get(i));
		}
		this.repaint();
	}
	
	private Vector<JScrollPane> components;

	private JPanel getNewContainer(SensorType type) {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		JScrollPane jScrollPane = new JScrollPane();
		
		jScrollPane.setLocation(new Point(getNextX(),getNextY()));
		jScrollPane.setSize(defaultDimension(type,panel));
		System.out.println(jScrollPane.getBounds());
		jScrollPane.setPreferredSize(defaultDimension(type,panel));
		jScrollPane.setViewportView(panel);
		jScrollPane.setBorder(BorderFactory.createEmptyBorder());
		jScrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		components.add(jScrollPane);
		return panel;
	}

	private int getNextX() {
		return 110;
	}

	
	private Dimension defaultDimension(SensorType type,JPanel p) {
		Dimension d=new Dimension();
		switch (type) {
		case WINDOW_SENSOR:
			d.setSize(110, d_height);
			break;
		case PR_SENSOR:
			d.setSize(d_width,d_height);
			break;
		default:
			d.setSize(d_width,d_height);
			p.setBorder(BorderFactory.createCompoundBorder(BorderFactory
					.createBevelBorder(BevelBorder.LOWERED), BorderFactory
					.createBevelBorder(BevelBorder.RAISED)));
			break;
		}
		return d;
	}

	
	private int getNextY() {
		int y=components.size()*55+20;		
		return y;
	}

	private void buildTemp(Sensor s) {
		TemperatureScale ts=new TemperatureScale();
		ts.setPanel(getNewContainer(s.getType()));
		WriterCollection.allWriters.add(ts);
		s.addScaleWriter(ts);
	}
	
	public static void main(String args[])
	{
		JFrame main=new JFrame();
		main.setSize(900,600);
		SensorPanel sp=new SensorPanel();
		main.add(sp);
		Vector<Sensor> sensorList=new Vector<Sensor>();
		Room r=new Room();
		Sensor s=new Sensor();
		r.addSensor(s);
		s.setType(SensorType.TEMPERATURE_SENSOR);
		sensorList.add(s);
		sp.addSensors(sensorList);
		sp.build();		
		WriterCollection.allWriters.get(0).runWorker();
		for(int i=0;i<2000;i++)
		{
			s.getSensorData(r, i);
		}
		WriterCollection.allWriters.get(0).finished();
		main.setVisible(true);
	}

}
